% File to open, convert, analyse and save droplet data.  Must have the file
% Anisotropy_drop.py in the same folder.  

%% convert txt to dat %%

% remember to change the name of the .txt file to the one you want to
% analyse.  You can start typing then press tab to get a list of options.

data=importdata("06242023_In droplet insulin cal curve online mixed_100nM i   3.txt");
save DropletTest.dat data -ascii

%% Run Python Script %%

system("python Anisotropy_drop.py")

%% Load Data %%

dropdata=importdata("dropdata.txt");
FFT1=importdata("FFT.txt");
PMT1_stats1=importdata("PMT1_stats.txt");
PMT1_1=importdata("PMT1.txt");

%% Plot %%

figure
plot(dropdata(:,2))
title("PMT1")

figure
plot(FFT1(2,:),FFT1(1,:))
title("Frequency of Droplet Generation")

%% Clean Data %%

% Removes the first 200 data points, you can change this by changing the
% value of cut bellow.

cut=10;

% Any droplet that was longer than 600ms is removed.  You can change this
% threshold if your droplet generation is different.  The picked droplets
% are saved.

pick=dropdata(cut:end,1)<60;
pick=dropdata(cut:end,1)>5;
picked=pick.*dropdata(cut:end,2);
pickvals=nonzeros(picked);

figure
plot(pickvals)
title("PMT1 averages for each droplet after cleaning")

%% RSD for Flourescent Intensity %%

% Calculates the average, stdv and rsd of the PMT1 from your cleaned
% droplet data

ave=mean(pickvals);
stdv=std(pickvals);
rsd=stdv/ave;

%% RSD for Droplet Formation Frequency %%

% The same steps occur as for the PMT1 cleaning and RSD calculations but
% for the frequency.

pickedfreq=pick.*dropdata(cut:end,1);
pickvalfreqs=nonzeros(pickedfreq);

avefreq=mean(pickvalfreqs);
stdvfreq=std(pickvalfreqs);
rsquaredfreq=stdvfreq/avefreq;

%% RSD for Droplet Anisotropy %%

% The same steps occur as for the PMT1 cleaning and RSD calculations but
% for the Anisotropy.

pickedani=pick.*dropdata(cut:end,8);
pickvalani=nonzeros(pickedani);

aveani=mean(pickvalani);
stdvani=std(pickvalani);
rsquaredani=stdvani/aveani;

%% Save It %%

%save('Data')